<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * etc:基准测试类
 * author:deepe.xiao
 * update: 2018-10-10
 */


class Mark {

	var $marker = array();
	var $meminfo = array();
	
	function mark($name)
	{
		$this->marker[$name] = microtime();
		$this->meminfo[$name] = memory_get_usage();
	}
	
	function mark_time($point1 = '', $point2 = '', $decimals = 4)
	{
		if ($point1 == '')
		{
			return '{elapsed_time}';
		}

		if ( ! isset($this->marker[$point1]))
		{
			return '';
		}

		if ( ! isset($this->marker[$point2]))
		{
			$this->marker[$point2] = microtime();
		}

		list($sm, $ss) = explode(' ', $this->marker[$point1]);
		list($em, $es) = explode(' ', $this->marker[$point2]);

		return number_format(($em + $es) - ($sm + $ss), $decimals);
	}

	function memory_usage($point1 = '', $point2 = '')
	{
		// bytes -> MB
		$mem_min = round(($this->meminfo[$point2] - $this->meminfo[$point1]) / 1024 / 1024,5);
		return array($mem_min,$this->meminfo[$point2]);
	}

}
